<cfcomponent output="false">
	
	<!--- --->
	
	<cffunction name="getOptions" access="public" output="false" returntype="struct">
		<cfargument name="key" required="false" default="" />
		<cfargument name="userID" required="false" default="" />
		
		<cfset var local = {} />
		
		<cfset local.options = $.run.it('optionService.get#key#Options') />
		<!--- normally, options are loaded into the request for the logged in user --->
		<!--- for api access, load the options for the userID if passed in --->
		<cfif arguments.userID eq "">
			<cfset local.userAllOptions = request.options />
		<cfelse>
			<cfset local.userAllOptions = loadOptions(userID=arguments.userID) />
		</cfif>
		<cfset local.userOptions = {} />

		<cfif structKeyExists(local.userAllOptions,arguments.key)>
			<cfset local.userOptions = local.userAllOptions[arguments.key] />
		</cfif>
		<cfset structAppend(local.options,local.userOptions) />
		
		<cfreturn local.options />
		
	</cffunction>

	<!--- --->
	
	<cffunction name="saveOptions" access="public" output="false" returntype="struct">
		<cfargument name="key" required="true" />
		<cfargument name="args" required="true" />
		<cfargument name="userID" required="false" default="#request.session.user.id#" />
		
		<cfset var local = {} />
		
		<!--- get user.options --->
		<cfset local.userOptions = request.options />
		
		<!--- merge with save struct --->
		<cfif structKeyExists(local.userOptions,arguments.key)>
			<cfset structAppend(local.userOptions[arguments.key],arguments.args) />
		<cfelse>
			<cfset local.userOptions[arguments.key] = arguments.args />
		</cfif>

		<!--- remove blanks --->
		<cfloop collection="#arguments.args#" item="local.item" index="local.i">
			<cfif not isStruct(arguments.args[local.i]) and arguments.args[local.i] eq "" and structKeyExists(local.userOptions,arguments.key)>
				<cfset structDelete(local.userOptions[arguments.key],local.i) />
			</cfif>
		</cfloop>
		
		<!--- check against base options to remove unused keys --->
		<cfset local.baseOptions = $.run.it('optionService.get#key#Options') />
		<cfset local.keysToDelete = "" />
		<cfloop collection="#local.userOptions[arguments.key]#" index="local.key" item="local.value">
			<cfif not structKeyExists(local.baseOptions,local.key)>
				<cfset local.keysToDelete = listAppend(local.keysToDelete,local.key) />
			</cfif>
		</cfloop>


		<cfloop list="#local.KeysToDelete#" index="local.i" item="local.item">
			<cfset structDelete(local.userOptions[arguments.key],local.item) />
		</cfloop>
				
		<!--- serialize and save to userOptions --->
		<cfset $.database.save(
			table="user",
			id=arguments.userID,
			options=serializeJSON(local.userOptions)
		) />
		
		<!--- invalidate cache on this user --->
		<cfset $.run.it('craftivityService.getUser',{userID=arguments.userID,invalidateCache=true}) />
		
		<!--- update request with new options --->
		<cfset request.options = local.userOptions />
		
		<!--- return updated options--->
		<cfset local.options = getOptions(key=arguments.key,userID=arguments.userID) />
		
		<cfreturn local.options />
	</cffunction>

	<!--- --->

	<cffunction name="getCalendarOptions" access="public" output="false" returntype="struct">
		
		<cfset var local = {} />
		
		<cfset local.options = {
			condensed=0,
			refresh_token="",
			access_token="",
			access_token_expire="",
			google_calendar_ids="", <!--- user's currently selected calendars --->
			last_synced_on="",
			craftivity_calendar_id="", <!--- the craftivity calendar ID (not deselectable) (all craftivity created events go here) --->
			google_calendars={}, <!--- contains a struct of google calendars and their syncTokens --->
			initial_sync_complete="false",
			filterDays="",
			filterCategories="",
			pushThread="",
			pullThread="",
			force_google_sync=0,
			dstString="03/08-11/02"
		} />
		
		<cfreturn local.options />
		
	</cffunction>

	<!--- --->
	
	<cffunction name="getWorkweekOptions" access="public" output="false" returntype="struct">
		
		<cfset var local = {} />
		
		<cfset local.options = {
			start_time="08",
			end_time="18",
			workdays="0,1,2,3,4,5,6",
			categories="Work,Important,Break,Personal,Misc 1,Misc 2",
			first_day="0"
		} />
		
		<cfreturn local.options />
		
	</cffunction>

	<!--- --->
	
	<cffunction name="getWorksheetOptions" access="public" output="false" returntype="struct">
		
		<cfset var local = {} />
		
		<cfset local.options = {
			option1="",
			option2=""
		} />
		
		<cfreturn local.options />
		
	</cffunction>

	<!--- --->
	
	<cffunction name="getHopscotchOptions" access="public" output="false" returntype="struct">
		
		<cfset var local = {} />
		
		<cfset local.keys = [
			"dashboard1",
			"calendar1",
			"calendar2",
			"calendar3",
			"goal1",
			"workweek1",
			<!---  --->
			"dashboardVid1",
			"calendarVid1",
			"goalVid1",
			"workweekVid1"
		] />
		
		<cfset local.options = {} />
		<cfloop array="#local.keys#" index="local.key">
			<cfset local.options[local.key] = false />
			<cfset local.options["#local.key#_fin"] = "" />
		</cfloop>
		
		<cfreturn local.options />
		
	</cffunction>

	<!--- --->
	
	<cffunction name="getSecretaryOptions" access="public" output="false" returntype="struct">
		
		<cfset var local = {} />
		
		<cfset local.options = {
			sound="1",
			speech="1"
		} />
		
		<cfreturn local.options />
		
	</cffunction>

	<!--- --->
	
	<cffunction name="getOptionsTemplate" access="public" output="false" returntype="struct">
		
		<cfset var local = {} />
		
		<cfset local.options = {} />
		<cfset local.options.worksheet = getWorksheetOptions() />
		<cfset local.options.calendar = getCalendarOptions()>
		
		<cfreturn local.options />
		
	</cffunction>

	<!--- --->
	
	<cffunction name="loadOptions" access="public" output="false" returntype="struct">
		<cfargument name="userID" required="true" />
		
		<cfset var local = {} />
		
		<cfif len(arguments.userID) eq 32>
			<cfset local.user = $.run.it(method='craftivityService.getUser',args={userID=arguments.userID}) />
			
			<cfif local.user.recordcount gt 0>
				<cfset local.options = deserializeJSON(local.user.options) />
				<!--- attach the force_google_sync flag here --->
				<cfif not structKeyExists(local.options,'calendar')>
					<cfset local.options.calendar = {} />
				</cfif>
				<cfset local.options.calendar.force_google_sync = local.user.force_google_sync />
			<cfelse>
				<cfset local.options = getOptionsTemplate() />
			</cfif>
		<cfelse>
			<cfset local.options = getOptionsTemplate() />
		</cfif>
		
		<cfreturn local.options />
		
	</cffunction>

	<!--- --->
	
</cfcomponent>